package com.example.servicea.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.common.msg.R;
import com.example.common.util.AnnotationUtils;
import com.example.common.util.ReflectionUtils;
import com.example.servicea.dao.*;
import com.example.servicea.entity.*;
import com.example.servicea.service.LoginService;
import com.example.servicea.utils.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @author hutu.
 * @date: 2017/11/6 10:38
 */
@Service("loginServiceImpl")
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper RoleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserPermissionMapper usePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    JwtUtils jwtUtils;
    @Override
    public R login(String username, String password) {
        List<UserEntity> list = userMapper.selectList(new EntityWrapper<UserEntity>().eq("name", username.trim()));
        UserEntity user = (list!=null&&list.size()>0)?list.get(0):null;
        CallerInfo callerInfo = new CallerInfo();
        String token = "";
        String permission = "";
        if(user==null){return null;}else{
            callerInfo.uid=user.getId()+"";
            callerInfo.role = "";

            List<UserRoleEntity> userRoleList = userRoleMapper.selectList(new EntityWrapper<UserRoleEntity>().eq("userId",user.getId()));
            List<String> roleIds = new ArrayList<>();

            for (int i = 0; i <userRoleList.size(); i++) {
                roleIds.add(userRoleList.get(i).getRoleId());
            }
            String[] longs = roleIds.toArray(new String[roleIds.size()]);

            List<RoleEntity> roleList = RoleMapper.selectList(new EntityWrapper<RoleEntity>().in("id",longs));
            for (int i = 0; i <roleList.size(); i++) {
                callerInfo.role = StringUtils.isEmpty(callerInfo.role)?roleList.get(i).getName():callerInfo.role+"/"+ roleList.get(i).getName();
            }
            permission = callerInfo.role;
            token =jwtUtils.generateToken(callerInfo.uid);
        }
        return R.ok().put("token",token).put("permission",permission).put("user",user);
    }
    @Override
    public List<String> getUserPermiss(String userId) {
        List<UserRoleEntity> userRoleList = userRoleMapper.selectList(new EntityWrapper<UserRoleEntity>()
                .eq(AnnotationUtils.getAnnotationValue(UserRoleEntity.class,"userId").toString(),userId));
        ReflectionUtils.getAccessibleField(UserRoleEntity.class,userId);
        List<UserPermissionEntity> userPermissionList = usePermissionMapper.selectList(new EntityWrapper<UserPermissionEntity>()
                .eq("userId",userId));
        List<String> roleIds = new ArrayList<>();
        List<String> permissionIds = new ArrayList<>();

        for (int i = 0; i <userRoleList.size(); i++) {
            roleIds.add(userRoleList.get(i).getRoleId());
        }
        String[] roleLongs = roleIds.toArray(new String[roleIds.size()]);

        List<RolePermissionEntity> rolePermissionEntityList = rolePermissionMapper.selectList(new EntityWrapper<RolePermissionEntity>()
                .in("roleId",roleLongs));
        for (int i = 0; i <rolePermissionEntityList.size(); i++) {
            permissionIds.add(rolePermissionEntityList.get(i).permissionId);
        }
        for (int i = 0; i <userPermissionList.size(); i++) {
            permissionIds.add(userPermissionList.get(i).permissionId);
        }
        List<PermissionEntity> permissionEntityList = permissionMapper.selectList(new EntityWrapper<PermissionEntity>()
                .in("permissionId",permissionIds));
        List<String> reList = new ArrayList<>();

        for (int i = 0; i < permissionEntityList.size(); i++) {
            if(StringUtils.isNotBlank(permissionEntityList.get(i).permissionValue)){
                reList.add(permissionEntityList.get(i).permissionValue);
            }
        }
        return reList;
    }

    @Override
    public String[] getRolesByUserId(String userId) {
        List<UserRoleEntity> userRoleList = userRoleMapper.selectList(new EntityWrapper<UserRoleEntity>().eq("userId",userId));
        String[] roleIds = new String[userRoleList.size()];
        for (int i = 0; i <userRoleList.size(); i++) {
            roleIds[i] = userRoleList.get(i).roleId;
        }
        return roleIds;
    }

    @Override
    public String[] getPermissionsByRoleId(String roleId) {
        String[] roleIds = roleId.split(",");
        List<RolePermissionEntity> List = rolePermissionMapper.selectList(new EntityWrapper<RolePermissionEntity>().in("roleId", roleIds));
        String[] permissionIds = new String[List.size()];
        for (int i = 0; i < List.size(); i++) {
            permissionIds[i] = List.get(i).permissionId;
        }
        Set<String> set = new HashSet<>();
        for(int i=0;i<permissionIds.length;i++){
            set.add(permissionIds[i]);
        }
        String[] arrayResult = set.toArray(new String[set.size()]);
        String[] permissionIds0 = new String[set.size()];
        for (int i = 0; i < arrayResult.length; i++) {
            permissionIds0[i] = arrayResult[i];
        }
        return permissionIds0;
    }
}
